<?php

namespace plugin\ycMassage\app\admin\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use Exception;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageMchBill;
use plugin\ycMassage\app\model\MassageMoneyRegister;
use plugin\ycMassage\app\model\MassageOrders;
use plugin\ycMassage\app\model\MassageShopBill;
use plugin\ycMassage\app\model\MassageUser;
use plugin\ycMassage\app\model\MassageUserAgentBill;
use plugin\ycMassage\app\model\MassageUserBill;
use plugin\ycMassage\app\model\MassageUserChannelsBill;
use plugin\ycMassage\app\model\MassageUserDealerBill;
use plugin\ycMassage\app\model\MassageUserTechnicianBill;
use plugin\ycMassage\app\model\MassageUserWallet;
use plugin\ycMassage\utils\enum\MoneyAction;
use plugin\ycMassage\utils\enum\OrdersType;
use plugin\ycMassage\utils\enum\PayType;
use plugin\ycMassage\utils\Str;
use support\Request;
use think\facade\Db;

class UserWalletController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->config('size','');
        $builder->config('showOverflow',false);
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addScreen('uid', 'input', 'UID', [
            'placeholder' => 'UID搜索'
        ]);
        $builder->addScreen('mobile', 'input', '手机号', [
            'placeholder' => '手机号搜索'
        ]);
        $builder->addRightButton('update', '修改', [
            'api'    => $this->pluginPrefix . '/admin/UserWallet/update',
            'path' => '/UserWallet/update',
            'type'      => 'modal'
        ], [], [
            'type' => 'primary',
            'link' => true
        ]);
        $builder->addRightButton('money', '代客充值', [
            'api'    => $this->pluginPrefix . '/admin/UserWallet/money',
            'path' => '/UserWallet/money',
            'type'      => 'modal'
        ], [], [
            'type' => 'success',
            'link' => true
        ]);
        $builder->addRightButton('log', '明细', [
            'api'    => $this->pluginPrefix . '/admin/UserWallet/log',
            'path' => '/UserWallet/log',
            'aliasParams' => [
                'id' => 'uid'
            ]
        ], [], [
            'type' => 'info',
            'link' => true
        ]);
        $builder->addColumn('uid', 'UID', [
            'width' => 90
        ])
            ->addColumn('mobile', '手机号', [
                'width' => 120
            ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120
            ])
            ->addColumnEle('money_html','余额',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('integral_html','积分',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('technician_html','技师',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('agent_html','代理',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('mch_html','商户',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('shop_html','门店',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('channels_html','渠道',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('dealer_html','分销',[
                'params' => [
                    'type' => 'html',
                ],
            ])
            ->addColumnEle('salesman_html','业务员',[
                'params' => [
                    'type' => 'html',
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['u.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if (!empty($uid)) {
            $where[] = ['w.uid', '=', $uid];
        }
        $mobile = $request->get('mobile');
        if (!empty($mobile)) {
            $where[] = ['u.mobile', 'like', '%' . $mobile . '%'];
        }
        $data = MassageUserWallet::where($where)
            ->alias('w')
            ->join('massage_user u', 'w.uid=u.id', 'LEFT')
            ->order('u.id desc')
            ->field('w.*,u.id,u.mobile,u.nickname,u.headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
                $money_html='<div class="el-text el-text--danger">可用：'.$item->money.'</div>';
                $money_html.='<div class="el-text el-text--small">总充值：'.$item->money_sum.'</div>';
                $item->money_html=$money_html;

                $integral_html='<div class="el-text el-text--danger">可用：'.$item->integral.'</div>';
                $integral_html.='<div class="el-text el-text--small">总积分：'.$item->integral_sum.'</div>';
                $item->integral_html=$integral_html;
                
                $technician_html='<div class="el-text el-text--danger">可用：'.$item->technician_money.'</div>';
                $technician_html.='<div class="el-text el-text--success">冻结：'.$item->technician_freeze_money.'</div>';
                $technician_html.='<div class="el-text el-text--small">总余额：'.$item->technician_money_sum.'</div>';
                $item->technician_html=$technician_html;
                
                $agent_html='<div class="el-text el-text--danger">可用：'.$item->agent_money.'</div>';
                $agent_html.='<div class="el-text el-text--success">冻结：'.$item->agent_freeze_money.'</div>';
                $agent_html.='<div class="el-text el-text--small">总余额：'.$item->agent_money_sum.'</div>';
                $item->agent_html=$agent_html;

                $mch_html='<div class="el-text el-text--danger">可用：'.$item->mch_money.'</div>';
                $mch_html.='<div class="el-text el-text--success">冻结：'.$item->mch_freeze_money.'</div>';
                $mch_html.='<div class="el-text el-text--small">总余额：'.$item->mch_money_sum.'</div>';
                $item->mch_html=$mch_html;

                $shop_html='<div class="el-text el-text--danger">可用：'.$item->shop_money.'</div>';
                $shop_html.='<div class="el-text el-text--success">冻结：'.$item->shop_freeze_money.'</div>';
                $shop_html.='<div class="el-text el-text--small">总余额：'.$item->shop_money_sum.'</div>';
                $item->shop_html=$shop_html;

                $channels_html='<div class="el-text el-text--danger">可用：'.$item->channels_money.'</div>';
                $channels_html.='<div class="el-text el-text--success">冻结：'.$item->channels_freeze_money.'</div>';
                $channels_html.='<div class="el-text el-text--small">总余额：'.$item->channels_money_sum.'</div>';
                $item->channels_html=$channels_html;

                $dealer_html='<div class="el-text el-text--danger">可用：'.$item->dealer_money.'</div>';
                $dealer_html.='<div class="el-text el-text--success">冻结：'.$item->dealer_freeze_money.'</div>';
                $dealer_html.='<div class="el-text el-text--small">总余额：'.$item->dealer_money_sum.'</div>';
                $item->dealer_html=$dealer_html;

                $salesman_html='<div class="el-text el-text--danger">可用：'.$item->salesman_money.'</div>';
                $salesman_html.='<div class="el-text el-text--success">冻结：'.$item->salesman_freeze_money.'</div>';
                $salesman_html.='<div class="el-text el-text--small">总余额：'.$item->salesman_money_sum.'</div>';
                $item->salesman_html=$salesman_html;
            });
        return $this->successRes($data);
    }
    public function update(Request $request)
    {
        $uid = $request->get('id');
        if ($request->method() === 'POST') {
            $D = $request->post();
            $uid = $D['uid'];
            Db::startTrans();
            try {
                $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->lock(true)->find();
                if (!$MassageUserWallet) {
                    $MassageUserWallet = new MassageUserWallet();
                    $MassageUserWallet->uid = $uid;
                    $MassageUserWallet->saas_appid = $request->saas_appid;
                    $MassageUserWallet->money = 0;
                    $MassageUserWallet->money_sum = 0;
                    $MassageUserWallet->integral = 0;
                    $MassageUserWallet->integral_sum = 0;
                    $MassageUserWallet->technician_money = 0;
                    $MassageUserWallet->technician_freeze_money = 0;
                    $MassageUserWallet->technician_money_sum = 0;
                    $MassageUserWallet->agent_money = 0;
                    $MassageUserWallet->agent_freeze_money = 0;
                    $MassageUserWallet->agent_money_sum = 0;
                }
                if ($D['money'] > 0) {
                    if (empty($D['money_remarks'])) {
                        throw new \Exception('请填写用户余额变动日志');
                    }
                    $MassageUserBill = new MassageUserBill();
                    $MassageUserBill->uid = $uid;
                    $MassageUserBill->saas_appid = $MassageUserWallet->saas_appid;
                    $moeny = abs($D['money']);
                    $MassageUserBill->money = $moeny;
                    if ($D['money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageUserBill->old_money = $MassageUserWallet->money;
                        $MassageUserBill->new_money = $MassageUserWallet->money + $moeny;
                        $MassageUserWallet->money = Db::raw('money+' . $moeny);
                        $MassageUserWallet->money_sum = Db::raw('money_sum+' . $moeny);
                    } else {
                        if ($MassageUserWallet->money < $moeny) {
                            throw new \Exception('余额不足');
                        }
                        $MassageUserBill->old_money = $MassageUserWallet->money;
                        $MassageUserBill->new_money = $MassageUserWallet->money - $moeny;
                        $MassageUserWallet->money = Db::raw('money-' . $moeny);
                    }
                    $MassageUserBill->action = $D['money_state'];
                    $MassageUserBill->remarks = $D['money_remarks'];
                    $MassageUserBill->save();
                }
                if ($D['integral'] > 0) {
                    $integral = abs($D['integral']);
                    if ($D['integral_state'] == MoneyAction::INCREASE['value']) {
                        $MassageUserWallet->integral = Db::raw('integral+' . $integral);
                        $MassageUserWallet->integral_sum = Db::raw('integral_sum+' . $integral);
                    } else {
                        $MassageUserWallet->integral = Db::raw('integral-' . $integral);
                    }
                }
                if ($D['technician_money'] > 0) {
                    if (empty($D['technician_money_remarks'])) {
                        throw new \Exception('请填写技师余额变动日志');
                    }
                    $MassageUserTechnicianBill = new MassageUserTechnicianBill();
                    $MassageUserTechnicianBill->uid = $uid;
                    $MassageUserTechnicianBill->saas_appid = $MassageUserWallet->saas_appid;
                    $technician_money = abs($D['technician_money']);
                    $MassageUserTechnicianBill->money = $technician_money;
                    if ($D['technician_money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageUserTechnicianBill->old_money = $MassageUserWallet->technician_money;
                        $MassageUserTechnicianBill->new_money = $MassageUserWallet->technician_money + $technician_money;
                        $MassageUserWallet->technician_money = Db::raw('technician_money+' . $technician_money);
                        $MassageUserWallet->technician_money_sum = Db::raw('technician_money_sum+' . $technician_money);
                    } else {
                        $MassageUserTechnicianBill->old_money = $MassageUserWallet->technician_money;
                        $MassageUserTechnicianBill->new_money = $MassageUserWallet->technician_money - $technician_money;
                        $MassageUserWallet->technician_money = Db::raw('technician_money-' . $technician_money);
                    }
                    $MassageUserTechnicianBill->action = $D['technician_money_state'];
                    $MassageUserTechnicianBill->remarks = $D['technician_money_remarks'];
                    $MassageUserTechnicianBill->save();
                }
                if ($D['agent_money'] > 0) {
                    if (empty($D['agent_money_remarks'])) {
                        throw new \Exception('请填写代理余额变动日志');
                    }
                    $MassageUserAgentBill = new MassageUserAgentBill();
                    $MassageUserAgentBill->uid = $uid;
                    $MassageUserAgentBill->saas_appid = $MassageUserWallet->saas_appid;
                    $agent_money = abs($D['agent_money']);
                    $MassageUserAgentBill->money = $agent_money;
                    if ($D['agent_money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageUserAgentBill->old_money = $MassageUserWallet->agent_money;
                        $MassageUserAgentBill->new_money = $MassageUserWallet->agent_money + $agent_money;
                        $MassageUserWallet->agent_money = Db::raw('agent_money+' . $agent_money);
                        $MassageUserWallet->agent_money_sum = Db::raw('agent_money_sum+' . $agent_money);
                    } else {
                        $MassageUserAgentBill->old_money = $MassageUserWallet->agent_money;
                        $MassageUserAgentBill->new_money = $MassageUserWallet->agent_money - $agent_money;
                        $MassageUserWallet->agent_money = Db::raw('agent_money-' . $agent_money);
                    }
                    $MassageUserAgentBill->action = $D['agent_money_state'];
                    $MassageUserAgentBill->remarks = $D['agent_money_remarks'];
                    $MassageUserAgentBill->save();
                }
                if($D['mch_money']>0){
                    if (empty($D['mch_money_remarks'])) {
                        throw new \Exception('请填写商户余额变动日志');
                    }
                    $MassageMchBill = new MassageMchBill();
                    $MassageMchBill->uid = $uid;
                    $MassageMchBill->saas_appid = $MassageUserWallet->saas_appid;
                    $mch_money = abs($D['mch_money']);
                    $MassageMchBill->money = $mch_money;
                    if ($D['mch_money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageMchBill->old_money = $MassageUserWallet->mch_money;
                        $MassageMchBill->new_money = $MassageUserWallet->mch_money + $mch_money;
                        $MassageUserWallet->mch_money = Db::raw('mch_money+' . $mch_money);
                        $MassageUserWallet->mch_money_sum = Db::raw('mch_money_sum+' . $mch_money);
                    } else {
                        $MassageMchBill->old_money = $MassageUserWallet->mch_money;
                        $MassageMchBill->new_money = $MassageUserWallet->mch_money - $mch_money;
                        $MassageUserWallet->mch_money = Db::raw('mch_money-' . $mch_money);
                    }
                    $MassageMchBill->action = $D['mch_money_state'];
                    $MassageMchBill->remarks = $D['mch_money_remarks'];
                    $MassageMchBill->save();
                }
                if($D['shop_money']>0){
                    if (empty($D['shop_money_remarks'])) {
                        throw new \Exception('请填写门店余额变动日志');
                    }
                    $MassageShopBill = new MassageShopBill();
                    $MassageShopBill->uid = $uid;
                    $MassageShopBill->saas_appid = $MassageUserWallet->saas_appid;
                    $shop_money = abs($D['shop_money']);
                    $MassageShopBill->money = $shop_money;
                    if ($D['shop_money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageShopBill->old_money = $MassageUserWallet->shop_money;
                        $MassageShopBill->new_money = $MassageUserWallet->shop_money + $shop_money;
                        $MassageUserWallet->shop_money = Db::raw('shop_money+' . $shop_money);
                        $MassageUserWallet->shop_money_sum = Db::raw('shop_money_sum+' . $shop_money);
                    } else {
                        $MassageShopBill->old_money = $MassageUserWallet->shop_money;
                        $MassageShopBill->new_money = $MassageUserWallet->shop_money - $shop_money;
                        $MassageUserWallet->shop_money = Db::raw('shop_money-' . $shop_money);
                    }
                    $MassageShopBill->action = $D['shop_money_state'];
                    $MassageShopBill->remarks = $D['shop_money_remarks'];
                    $MassageShopBill->save();
                }
                if($D['channels_money']>0){
                    if (empty($D['channels_money_remarks'])) {
                        throw new \Exception('请填写渠道余额变动日志');
                    }
                    $MassageUserChannelsBill = new MassageUserChannelsBill();
                    $MassageUserChannelsBill->uid = $uid;
                    $MassageUserChannelsBill->saas_appid = $MassageUserWallet->saas_appid;
                    $channels_money = abs($D['channels_money']);
                    $MassageUserChannelsBill->money = $channels_money;
                    if ($D['channels_money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageUserChannelsBill->old_money = $MassageUserWallet->channels_money;
                        $MassageUserChannelsBill->new_money = $MassageUserWallet->channels_money + $channels_money;
                        $MassageUserWallet->channels_money = Db::raw('channels_money+' . $channels_money);
                        $MassageUserWallet->channels_money_sum = Db::raw('channels_money_sum+' . $channels_money);
                    } else {
                        $MassageUserChannelsBill->old_money = $MassageUserWallet->channels_money;
                        $MassageUserChannelsBill->new_money = $MassageUserWallet->channels_money - $channels_money;
                        $MassageUserWallet->channels_money = Db::raw('channels_money-' . $channels_money);
                    }
                    $MassageUserChannelsBill->action = $D['channels_money_state'];
                    $MassageUserChannelsBill->remarks = $D['channels_money_remarks'];
                    $MassageUserChannelsBill->save();
                }
                if($D['dealer_money']>0){
                    if (empty($D['dealer_money_remarks'])) {
                        throw new \Exception('请填写分销余额变动日志');
                    }
                    $MassageUserDealerBill = new MassageUserDealerBill();
                    $MassageUserDealerBill->uid = $uid;
                    $MassageUserDealerBill->saas_appid = $MassageUserWallet->saas_appid;
                    $dealer_money = abs($D['dealer_money']);
                    $MassageUserDealerBill->money = $dealer_money;
                    if ($D['dealer_money_state'] == MoneyAction::INCREASE['value']) {
                        $MassageUserDealerBill->old_money = $MassageUserWallet->dealer_money;
                        $MassageUserDealerBill->new_money = $MassageUserWallet->dealer_money + $dealer_money;
                        $MassageUserWallet->dealer_money = Db::raw('dealer_money+' . $dealer_money);
                        $MassageUserWallet->dealer_money_sum = Db::raw('dealer_money_sum+' . $dealer_money);
                    } else {
                        $MassageUserDealerBill->old_money = $MassageUserWallet->dealer_money;
                        $MassageUserDealerBill->new_money = $MassageUserWallet->dealer_money - $dealer_money;
                        $MassageUserWallet->dealer_money = Db::raw('dealer_money-' . $dealer_money);
                    }
                    $MassageUserDealerBill->action = $D['dealer_money_state'];
                    $MassageUserDealerBill->remarks = $D['dealer_money_remarks'];
                    $MassageUserDealerBill->save();
                }
                $MassageUserWallet->save();
                Db::commit();
                return $this->success('修改成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('uid', 'hidden', 'UID', $uid);
        $builder->addDivider('余额操作');
        $builder->addRow('money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('积分操作');
        $builder->addRow('integral_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('integral', 'number', '积分', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 0,
                'controls' => false,
            ]
        ]);
        $builder->addRow('integral_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('技师余额操作');
        $builder->addRow('technician_money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('technician_money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('technician_money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('代理余额操作');
        $builder->addRow('agent_money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('agent_money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('agent_money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('商户余额操作');
        $builder->addRow('mch_money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('mch_money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('mch_money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('门店余额操作');
        $builder->addRow('shop_money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('shop_money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('shop_money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('渠道余额操作');
        $builder->addRow('channels_money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('channels_money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('channels_money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $builder->addDivider('分销余额操作');
        $builder->addRow('dealer_money_state', 'radio', '操作', '', [
            'col' => 12,
            'options' => MoneyAction::getOptions()
        ]);
        $builder->addRow('dealer_money', 'number', '金额', null, [
            'col' => 12,
            'props' => [
                'min' => 0,
                'precision' => 2,
                'controls' => false,
            ]
        ]);
        $builder->addRow('dealer_money_remarks', 'textarea', '变动日志', '', [
            'col' => 24,
        ]);
        $data = $builder->create();
        return $this->successRes($data);
    }
    public function money(Request $request)
    {
        $uid = $request->get('id');
        if ($request->method() === 'POST') {
            $D = $request->post();
            $uid = $D['uid'];
            Db::startTrans();
            try {
                $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->find();
                if (!$MassageUserWallet) {
                    $MassageUserWallet = new MassageUserWallet();
                    $MassageUserWallet->uid = $uid;
                    $MassageUserWallet->saas_appid = $request->saas_appid;
                    $MassageUserWallet->save();
                }
                $MassageUser = MassageUser::where(['id' => $uid])->find();
                $MassageMoneyRegister = MassageMoneyRegister::where(['id' => $D['rid']])->find();
                $money = $MassageMoneyRegister->money;
                $give_money = $MassageMoneyRegister->give_money;
                $MassageOrders = new MassageOrders();
                $MassageOrders->saas_appid = $request->saas_appid;
                $MassageOrders->uid = $MassageUser->id;
                $MassageOrders->alias_id = $MassageUser->id;
                $MassageOrders->origin_price = $money;
                $MassageOrders->price = $money;
                $MassageOrders->give_price = $give_money;
                if ($MassageUser->puid) {
                    $MassageOrders->puid = $MassageUser->puid;
                }
                $MassageOrders->title = "预充值{$money}元";
                if ($give_money > 0) {
                    $MassageOrders->title = "预充值{$money}元，赠送{$give_money}元";
                }
                $trade = date('YmdHis') . Str::random(6, 1);
                $tradeCreateNum = 0;
                while (MassageOrders::where('trade', $trade)->count() > 0) {
                    $trade = date('YmdHis') . Str::random(6, 1);
                    $tradeCreateNum++;
                    if ($tradeCreateNum > 10) {
                        throw new \Exception('生成订单失败', 1006);
                    }
                }
                $MassageOrders->trade = $trade;
                $MassageOrders->orders_type = OrdersType::MONEY['value'];
                $MassageOrders->expire_time = date('Y-m-d H:i:s', time() + 600);
                $MassageOrders->coupons = [];
                $MassageOrders->pay_type = PayType::ARTIFICIAL['value'];
                $MassageOrders->pay_time = date('Y-m-d H:i:s');
                $MassageOrders->save();
                Db::commit();
                try {
                    MassageOrders::complete($MassageOrders->trade);
                } catch (\Throwable $th) {
                    return $this->fail($th->getMessage());
                }
                return $this->success('充值成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }
        $options = [];
        $list = MassageMoneyRegister::where(['saas_appid' => $request->saas_appid])->select();
        foreach ($list as $item) {
            $options[] = [
                'label' => "充{$item->money}元，送{$item->give_money}元",
                'value' => $item->id,
            ];
        }
        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('uid', 'hidden', 'UID', $uid);
        $builder->addRow('rid', 'select', '套餐', '', [
            'col' => 24,
            'options' => $options
        ]);
        $data = $builder->create();
        return $this->successRes($data);
    }
    public function logGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addScreen('uid', 'input', 'UID', [
            'placeholder' => 'UID搜索'
        ]);
        $builder->addScreen('mobile', 'input', '手机号', [
            'placeholder' => '手机号搜索'
        ]);
        $builder->addColumn('uid', 'UID', [
            'width' => 90
        ])
            ->addColumn('mobile', '手机号', [
                'width' => 120
            ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120
            ])
            ->addColumn('money', '变动金额', [
                'width' => 120
            ])
            ->addColumn('old_money', '变动前', [
                'width' => 120
            ])
            ->addColumn('new_money', '变动后', [
                'width' => 120
            ])
            ->addColumnEle('action', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => MoneyAction::dictOptions(),
                    'style' => MoneyAction::dictOptions('style'),
                ],
            ])
            ->addColumn('remarks', '备注')
            ->addColumn('create_time', '时间', [
                'width' => 160
            ]);
        return $this->successRes($builder->create());
    }
    public function log(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['u.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if (!empty($uid)) {
            $where[] = ['log.uid', '=', $uid];
        }
        $mobile = $request->get('mobile');
        if (!empty($mobile)) {
            $where[] = ['u.mobile', 'like', '%' . $mobile . '%'];
        }
        $data = MassageUserBill::where($where)
            ->alias('log')
            ->join('massage_user u', 'log.uid=u.id', 'LEFT')
            ->order('log.id desc')
            ->field('log.*,u.mobile,u.nickname,u.headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
            });
        return $this->successRes($data);
    }
    public function technicianLogGetTable(Request $request)
    {
        return $this->logGetTable($request);
    }
    public function technicianLog(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['u.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if (!empty($uid)) {
            $where[] = ['log.uid', '=', $uid];
        }
        $mobile = $request->get('mobile');
        if (!empty($mobile)) {
            $where[] = ['u.mobile', 'like', '%' . $mobile . '%'];
        }
        $data = MassageUserTechnicianBill::where($where)
            ->alias('log')
            ->join('massage_user u', 'log.uid=u.id', 'LEFT')
            ->order('log.id desc')
            ->field('log.*,u.mobile,u.nickname,u.headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
            });
        return $this->successRes($data);
    }
    public function agentLogGetTable(Request $request)
    {
        return $this->logGetTable($request);
    }
    public function agentLog(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['u.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if (!empty($uid)) {
            $where[] = ['log.uid', '=', $uid];
        }
        $mobile = $request->get('mobile');
        if (!empty($mobile)) {
            $where[] = ['u.mobile', 'like', '%' . $mobile . '%'];
        }
        $data = MassageUserAgentBill::where($where)
            ->alias('log')
            ->join('massage_user u', 'log.uid=u.id', 'LEFT')
            ->order('log.id desc')
            ->field('log.*,u.mobile,u.nickname,u.headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
            });
        return $this->successRes($data);
    }
}
